<?php
class flickrAPI {
	public $api_key;
	public $format;
	public $user_id;
	
	function __construct($api_key = 'e59a90d4a27f3d83758a0fc10f57850f', $user_id = '75262407@N00', $format = 'php_serial'){	
		$this->api_key = $api_key;
		$this->format = $format;
		$this->user_id = $user_id;
	}
	
	function getRandomPhoto(){
		$params = array(
			'api_key'	=> $this->api_key,
			'method'	=> 'flickr.photos.search',
			'format'	=> $this->format,
			'user_id'	=> $this->user_id
		);		
		
		foreach ($params as $k => $v){
			$encoded_params[] = urlencode($k).'='.urlencode($v);
		}
		
		$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
		$rsp = file_get_contents($url);
		$photo_results = unserialize($rsp);
		$result_count = $photo_results['photos']['total'];
		$random_photo_num = rand(0, $result_count);
		$page_num = intval($random_photo_num / $photo_results['photos']['perpage']);
		$paged_random_photo_num = intval($random_photo_num % $photo_results['photos']['perpage']);

		if($page_num > 1){
			$params = array(
				'api_key'	=> $this->api_key,
				'method'	=> 'flickr.photos.search',
				'format'	=> $this->format,
				'user_id'	=> $this->user_id,
				'page'      => $page_num
			);		
			
			foreach ($params as $k => $v){
				$encoded_params[] = urlencode($k).'='.urlencode($v);
			}
			
			$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
			$rsp = file_get_contents($url);
			$photo_results = unserialize($rsp);
		}
		
		$photo_id = $photo_results['photos']['photo'][$paged_random_photo_num]['id'];
		return $photo_id; 
	}
	
	
}

/*
 <photo id="2733" secret="123456" server="12" isfavorite="0" license="3" rotation="90" originalsecret="1bc09ce34a" originalformat="png">
  <owner nsid="12037949754@N01" username="Bees" realname="Cal Henderson" location="Bedford, UK" />
  <title>orford_castle_taster</title>
  <description>hello!</description>
  <visibility ispublic="1" isfriend="0" isfamily="0" />
  <dates posted="1100897479" taken="2004-11-19 12:51:19" takengranularity="0" lastupdate="1093022469" />
  <permissions permcomment="3" permaddmeta="2" />
  <editability cancomment="1" canaddmeta="1" />
  <comments>1</comments>
  <notes>
    <note id="313" author="12037949754@N01" authorname="Bees" x="10" y="10" w="50" h="50">foo</note>
  </notes>
  <tags>
    <tag id="1234" author="12037949754@N01" raw="woo yay">wooyay</tag>
    <tag id="1235" author="12037949754@N01" raw="hoopla">hoopla</tag>
  </tags>
  <urls>
    <url type="photopage">http://www.flickr.com/photos/bees/2733/</url>
  </urls>
</photo>
 */

class flickrPhoto extends flickrAPI{
	
	public $photoId;
	public $farmId;
	public $serverId;
	public $secretId;
	public $isFavorite;
	public $license;
	public $rotation;
	public $originalSecret;
	public $originalFormat;
	public $title;
	public $description;
	public $dateTaken;
	public $lastUpdate;
	public $postedDate;
	public $photoFileURL;
	
	function __construct($photo_id){
		parent::__construct();
		$this->photoId = $photo_id;
		$this->configPhoto();
	}
	
	
	
	function display($attribute){
		?>
		<div id="flkr_img">
			<?php //var_dump($photo);?>
			<img src="<?php echo $this->photoFileURL ?>" style="<?php echo $attribute ?>" />
		</div>
		<?php
	}

	function configPhoto(){
		$params = array(
			'api_key'	=> $this->api_key,
			'method'	=> 'flickr.photos.getInfo',
			'photo_id'  => $this->photoId,
			'format'	=> $this->format,
			'user_id'	=> $this->user_id
		);		
		$encoded_params = array();		
		foreach ($params as $k => $v){	
			$encoded_params[] = urlencode($k).'='.urlencode($v);
		}
		
		$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);	
		$rsp = file_get_contents($url);		
		$photo = unserialize($rsp);
		
		$this->farmId = $photo['photo']['farm'];
		$this->serverId = $photo['photo']['server'];
		$this->secretId =  $photo['photo']['secret'];
		$this->license =  $photo['photo']['license'];
		$this->isFavorite = $photo['photo']['isfavorite'];
		$this->rotation = $photo['photo']['rotation'];
		$this->originalSecret = $photo['photo']['originalsecret'];
		$this->originalFormat = $photo['photo']['originalformat'];
		$this->title = $photo['photo']['title'][0]["_content"];
		$this->photoFileURL = "http://farm" . $this->farmId . ".static.flickr.com/". $this->serverId ."/". $this->photoId . "_" . $this->secretId .".jpg";
	}
	
}
?>